//
//  UILabel+Utils.m
//  天诚云
//
//  Created by Mac on 2017/11/6.
//  Copyright © 2017年 Mac. All rights reserved.
//

#import "UILabel+Utils.h"

@implementation UILabel (Utils)

- (void)setText:(NSString*)text lineSpacing:(CGFloat)lineSpacing {
    if (lineSpacing < 0.01 || !text) {
        self.text = text;
        return;
    }
    
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:text];
    [attributedString addAttribute:NSFontAttributeName value:self.font range:NSMakeRange(0, [text length])];
    
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    [paragraphStyle setLineSpacing:lineSpacing];
    [paragraphStyle setLineBreakMode:self.lineBreakMode];
    [paragraphStyle setAlignment:self.textAlignment];
    [attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [text length])];
    
    self.attributedText = attributedString;
}

- (void)setTopAlignmentWithText:(NSString *)text maxHeight:(CGFloat)maxHeight
{
    CGRect frame = self.frame;
//    CGSize size = [text sizeWithFont:self.font constrainedToSize:CGSizeMake(frame.size.width, maxHeight)];
//    frame.size = CGSizeMake(frame.size.width, size.height);

    CGSize size = [text boundingRectWithSize:CGSizeMake(400, 10000) // 用于计算文本绘制时占据的矩形块
                                                  options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading // 文本绘制时的附加选项
                                               attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:15]}        // 文字的属性
                                                  context:nil].size;
    frame.size = size;
    
    self.frame = frame;
    self.text = text;
}


@end
